import { defineStore } from 'pinia';
import { ref } from 'vue';
import {
  getBannerListAPI,
  getCategoryListAPI,
  getNewGoodsListAPI,
  getHotGoodsListAPI,
} from '@/apis/home';
import type { CategoryList, BannerList, GoodsList } from '@/types';
import request from '@/utils/http';

export const useHomeStore = defineStore(
  'home',
  () => {
    // 准备响应式数据
    const money = ref(14000);
    const categoryList = ref([] as CategoryList);
    const bannerList = ref([] as BannerList);
    const newGoodsList = ref([] as GoodsList);
    const hotGoodsList = ref([] as GoodsList);

    // 定义业务逻辑
    const getCategoryList = async () => {
      const res = await getCategoryListAPI();
      categoryList.value = res.data.result;
      console.log('categoryList.value', categoryList.value);
    };

    const getBannerList = async () => {
      const res = await getBannerListAPI();
      bannerList.value = res.data.result;
    };

    const getNewGoodsList = async () => {
      const res = await getNewGoodsListAPI();
      // console.log('/home/new', res);
      newGoodsList.value = res.data.result;
    };

    const getHotGoodsList = async () => {
      const res = await getHotGoodsListAPI();
      // console.log('/home/hot', res);
      hotGoodsList.value = res.data.result;
    };

    // 🎯记得 return 返回
    return {
      money,
      categoryList,
      bannerList,
      newGoodsList,
      hotGoodsList,
      getCategoryList,
      getBannerList,
      getNewGoodsList,
      getHotGoodsList,
    };
  },
  { persist: true },
);
